  MODULE TSGPAD_MOD
  implicit none
  contains

!######################################################################

!  SUBROUTINE GPAD2( Gxx, xx )
!  implicit none
!  real, intent(in),  dimension(:,:) :: Gxx
!  real, intent(out), dimension(:,:) :: xx
!  integer :: ix, jx
!  integer :: nx, nx2, nxp1
!
!  ix = SIZE( Gxx, 1 )
!  jx = SIZE( Gxx, 2 )
!
!  nx2  = SIZE( xx, 1 ) - SIZE( Gxx, 1 )
!  nx   = nx2 / 2
!  nxp1 = nx + 1
!
!  xx(nxp1:ix+nx,jx+nx:nxp1:-1) = Gxx(:,:)
!
!  xx(      1:nx,       nxp1:jx+nx ) = xx(ix+1:ix+nx, nxp1:jx+nx)
!  xx(ix+nxp1:ix+nx2,   nxp1:jx+nx ) = xx(nxp1:nx2,   nxp1:jx+nx)
!  xx(      1:ix+nx2,      1:nx    ) = xx(   1:ix+nx2,jx+1:jx+nx)
!  xx(      1:ix+nx2,jx+nxp1:jx+nx2) = xx(   1:ix+nx2,nxp1:nx2  )
!
!  end SUBROUTINE GPAD2

!######################################################################

!######################################################################

  SUBROUTINE GPAD1FLIP( Gxx, xx, iflip )
  implicit none
  integer, intent(in)                :: iflip
  real,    intent(in),  dimension(:) :: Gxx
  real,    intent(out), dimension(:) :: xx
  integer :: i, ix, ip
  integer :: nx, nx2, nxp1
  real    :: dx

  nx2  = SIZE( xx, 1 ) - SIZE( Gxx, 1 )
  nx   = nx2 / 2
  nxp1 = nx + 1

  ix = SIZE( Gxx )
  ip = SIZE(  xx ) + 1

  if( iflip .eq. 0 ) then
     xx(nxp1:ix+nx) = Gxx(:)
  else
     xx(ix+nx:nxp1:-1) = Gxx(:)
  end if

  dx = xx(nx+2) - xx(nx+1)

  do i = 1,nx
     xx(   i) =  xx(   nxp1) - ( nxp1 - i ) * dx
     xx(ip-i) =  xx(ip-nxp1) + ( nxp1 - i ) * dx
  end do

  end SUBROUTINE GPAD1FLIP

!######################################################################




!######################################################################

  SUBROUTINE GPAD2( Gxx, xx )
  implicit none
  real, intent(in),  dimension(:,:) :: Gxx
  real, intent(out), dimension(:,:) :: xx
  integer :: ix, jx, i, j
  integer :: nx, nx2, nxp1

  ix = SIZE( Gxx, 1 )
  jx = SIZE( Gxx, 2 )

  nx2  = SIZE( xx, 1 ) - SIZE( Gxx, 1 )
  nx   = nx2 / 2
  nxp1 = nx + 1

  xx(nxp1:ix+nx,nxp1:jx+nx) = Gxx(:,:) 

  do i = 1,nx
    xx(i,   nxp1:jx+nx) = xx(ix+i,nxp1:jx+nx)
    xx(i+ix+nx,nxp1:jx+nx) = xx(nx+i,nxp1:jx+nx)
  end do

  do j = 1,nx
    xx(1:ix+nx2,j   ) = xx(1:ix+nx2, nxp1)
    xx(1:ix+nx2,j+jx) = xx(1:ix+nx2,jx)
  end do

  end SUBROUTINE GPAD2

!######################################################################

  SUBROUTINE GPAD2_VAR( Gxx, xx )
  implicit none
  real, intent(in),  dimension(:,:) :: Gxx
  real, intent(out), dimension(:,:) :: xx
  integer :: ix, jx, i, j
  integer :: nxi, nx2i, nxp1i
  integer :: nxj, nx2j, nxp1j

  ix = SIZE( Gxx, 1 )
  jx = SIZE( Gxx, 2 )

  nx2i  = SIZE( xx, 1 ) - SIZE( Gxx, 1 )
  nxi   = nx2i / 2
  nxp1i = nxi + 1

  nx2j  = SIZE( xx, 2 ) - SIZE( Gxx, 2 )
  nxj   = nx2j / 2
  nxp1j = nxj + 1

  xx(nxp1i:ix+nxi,nxp1j:jx+nxj) = Gxx(:,:) 

!  do i = 1,nxi
!    xx(i       ,nxp1j:jx+nxj)     = xx(ix+i   ,nxp1j:jx+nxj)
!    xx(i+ix+nxi,nxp1j:jx+nxj)     = xx(nxi+i  ,nxp1j:jx+nxj)
!  end do
!
!  do j = 1,nxj
!    xx(1:ix+nx2i  ,j   ) = xx(1:ix+nx2i  , nxp1j)
!    xx(1:ix+nx2i  ,j+jx) = xx(1:ix+nx2i  , jx)
!  end do

  xx(nxp1i:ix+nxi,jx+nxj:nxp1j:-1) = Gxx(:,:)

  xx(       1:nxi    ,    nxp1j:jx+nxj ) = xx(ix+1 :ix+nxi , nxp1j:jx+nxj)
  xx(ix+nxp1i:ix+nx2i,    nxp1j:jx+nxj ) = xx(nxp1i:nx2i   , nxp1j:jx+nxj)
  xx(       1:ix+nx2i,        1:nxj    ) = xx(   1 :ix+nx2i,  jx+1:jx+nxj)
  xx(       1:ix+nx2i, jx+nxp1j:jx+nx2j) = xx(   1 :ix+nx2i, nxp1j:nx2j  )

  end SUBROUTINE GPAD2_VAR

!######################################################################

  SUBROUTINE GPAD1( Gxx, xx )
  implicit none
  real,    intent(in),  dimension(:) :: Gxx
  real,    intent(out), dimension(:) :: xx
  integer :: i, ix, ip 
  integer :: nx, nx2, nxp1
  real    :: dx

  nx2  = SIZE( xx ) - SIZE( Gxx )
  nx   = nx2 / 2
  nxp1 = nx + 1

  ix = SIZE( Gxx )
  ip = SIZE(  xx ) + 1

  xx(nxp1:ix+nx) = Gxx(:) 

  dx = xx(nx+2) - xx(nx+1)

  do i = 1,nx
     xx(   i) =  xx(   nxp1) - ( nxp1 - i ) * dx
     xx(ip-i) =  xx(ip-nxp1) + ( nxp1 - i ) * dx 
  end do

  end SUBROUTINE GPAD1

!######################################################################
  end MODULE TSGPAD_MOD
